﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommonUtility
{
    /// <summary>
    /// 本地时间与UnixTime之间的相互转化
    /// </summary>
    public class UnixTimeConverter
    {
        //UnixTime起始时间戳
        private static readonly DateTime startUnixTime = new DateTime(1970, 1, 1, 0, 0, 0);

        public static double ToUnixTime(DateTime dateTime)
        {
            TimeSpan ts = dateTime.ToUniversalTime() - startUnixTime;
            return ts.TotalSeconds;
        }

        public static DateTime ToDateTime(double unixTime)
        {
            double totalSenconds = unixTime;
            TimeSpan ts = TimeSpan.FromSeconds(totalSenconds);
            DateTime dt = startUnixTime + ts;
            DateTime localTime = dt.ToLocalTime();
            return localTime;
        }
    }
}
